package com.jrauto.CarAppBackend.service.impl;


import com.jrauto.CarAppBackend.entity.dto.MediaCheckDTO;
import com.jrauto.CarAppBackend.entity.dto.TextSecurityCheckDTO;
import com.jrauto.CarAppBackend.entity.vo.SecurityCheckResult;
import com.jrauto.CarAppBackend.service.SecurityCheckService;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

/**
 * @author : huiMing
 * Date : 2025年09月18日 17:04
 * @version V1.0
 */

@Service
public class SecurityCheckServiceImpl implements SecurityCheckService {

    @Resource
    private AccessTokenService accessTokenService;
    private final RestTemplate restTemplate = new RestTemplate();

    // 微信API地址
    private static final String TEXT_SECURITY_CHECK_URL = "https://api.weixin.qq.com/wxa/msg_sec_check?access_token=";
    private static final String MEDIA_SECURITY_CHECK_URL = "https://api.weixin.qq.com/wxa/media_check_async?access_token=";
    private static final Logger logger = LoggerFactory.getLogger(SecurityCheckService.class);


    @Override
    public SecurityCheckResult msgSecCheck(TextSecurityCheckDTO request)  {
        String url = TEXT_SECURITY_CHECK_URL + accessTokenService.getAccessToken();
        // 构建请求体
        String body = "{" +
                "\"content\":\""   + request.getContent() + "\"," +
                "\"openid\":\""    + request.getOpenid()  + "\"," +
                "\"scene\":"       + request.getScene()   + "," +
                "\"version\":"     + request.getVersion() +
                "}";

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<String> entity = new HttpEntity<>(body, headers);
        return restTemplate.postForObject(url, entity, SecurityCheckResult.class);
    }

    @Override
    public Map<String, Object> mediaCheckAsync(MediaCheckDTO request) {
        String url = MEDIA_SECURITY_CHECK_URL + accessTokenService.getAccessToken();
        String body = "{\"openid\":\""   + request.getOpenid()   + "\"," +
                "\"scene\":"       + request.getScene()    + "," +
                "\"version\":"     + request.getVersion()  + "," +
                "\"media_url\":\"" + request.getMediaUrl() + "\"," +
                "\"media_type\":"  + request.getMediaType() + "}";
        // 创建HTTP请求头，指定内容类型为JSON
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);

        // 将请求体对象和请求头封装到HttpEntity中
        HttpEntity<String> httpEntity = new HttpEntity<>(body, headers);

        return restTemplate.exchange(
                url,
                HttpMethod.POST,
                httpEntity,
                new ParameterizedTypeReference<Map<String, Object>>() {}
        ).getBody();
    }
}
